package com.fyyg.common.randomnumber;


/*
 * 抽奖概率控制
 */
public class RandomNumberUtil {
	public static double rate0 = 0.005;		//0:苹果6s	0.5%
	public static double rate1 = 0.05;  	//1:松子		5%
	public static double rate2 = 0.05;  	//2:榴莲		5% 
	public static double rate3 = 0.38;  	//3:手机支架	38%
	public static double rate4 = 0.005;  	//4:金条		0.5%
	public static double rate5 = 0.36;  	//5:没中奖	36%
	public static double rate6 = 0.05;  	//6:单门冰箱	5%
	public static double rate7 = 0.05;  	//7:充电宝	5% 
	public static double rate8 = 0.05;  	//8:镶钻吊坠	5%  
  
	/** 
	 * Math.random()产生一个double型的随机数，判断一下 
	 * 例如0出现的概率为%50，则介于0到0.50中间的返回0 
     * @return int 
     * 
     */  
	public static int PercentageRandom(){  
		double randomNumber;  
		randomNumber = Math.random();  
		if (randomNumber >= 0 && randomNumber <= rate0){  
			return 0;  
		}else if (randomNumber >= rate0 / 100 && randomNumber <= rate0 + rate1){  
			return 1;  
		}else if (randomNumber >= rate0 + rate1 && randomNumber <= rate0 + rate1 + rate2){  
			return 2;  
		}else if (randomNumber >= rate0 + rate1 + rate2 && randomNumber <= rate0 + rate1 + rate2 + rate3){  
			return 3;  
		}else if (randomNumber >= rate0 + rate1 + rate2 + rate3 && randomNumber <= rate0 + rate1 + rate2 + rate3 + rate4){  
			return 4;  
		}else if (randomNumber >= rate0 + rate1 + rate2 + rate3 + rate4 && randomNumber <= rate0 + rate1 + rate2 + rate3 + rate4 + rate5){  
			return 5;  
		}else if (randomNumber >= rate0 + rate1 + rate2 + rate3 + rate4 + rate5 && randomNumber <= rate0 + rate1 + rate2 + rate3 + rate4 + rate5+ rate6){  
			return 6;  
		}else if (randomNumber >= rate0 + rate1 + rate2 + rate3 + rate4 + rate5+ rate6 && randomNumber <= rate0 + rate1 + rate2 + rate3 + rate4 + rate5+ rate6+ rate7){  
			return 7;  
		}else if (randomNumber >= rate0 + rate1 + rate2 + rate3 + rate4 + rate5+ rate6+ rate7 && randomNumber <= rate0 + rate1 + rate2 + rate3 + rate4 + rate5+ rate6+ rate7+ rate8){  
			return 8;  
		} 
		return -1;  
	}  
  
 	/** 
 	 * 测试主程序 
     * @param agrs 
     */  
	public static void main(String[] agrs){  
		int rate0=0;
		int rate1=0;
		int rate2=0;
		int rate3=0;
		int rate4=0;
		int rate5=0;
		int rate6=0;
		int rate7=0;
		int rate8=0;
		//打印100个测试概率的准确性
		for (int i = 1; i <= 100; i++){
			switch (PercentageRandom()) {
			case 0:
				rate0++;
				break;
			case 1:
				rate1++;
				break;
			case 2:
				rate2++;
				break;
			case 3:
				rate3++;
				break;
			case 4:
				rate4++;
				break;
			case 5:
				rate5++;
				break;
			case 6:
				rate6++;
				break;
			case 7:
				rate7++;
				break;
			case 8:
				rate8++;
				break;
			}
		}  
		System.out.println("0  100次出现的次数："+rate0);
		System.out.println("1  100次 出现的次数："+rate1);
		System.out.println("2  100次出现的次数："+rate2);
		System.out.println("3  100次出现的次数："+rate3);
		System.out.println("4  100次出现的次数："+rate4);
		System.out.println("5  100次 出现的次数："+rate5);
		System.out.println("6  100次出现的次数："+rate6);
		System.out.println("7  100次出现的次数："+rate7);
		System.out.println("8  100次 出现的次数："+rate8);
	}  

}
